package com.gxuwz.medical.domain.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AgeUtil {
	
	  public static  Date parse(String strDate) throws Exception {  
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
          return sdf.parse(strDate);  
      }  
     //由出生日期获得年龄  
      public static  int getAge(Date birthDay) throws Exception {  
          Calendar cal = Calendar.getInstance();  
          if (cal.before(birthDay)) {  
              throw new IllegalArgumentException(  
                      "The birthDay is before Now.It's unbelievable!");  
          }  
          int yearNow = cal.get(Calendar.YEAR);  
          int monthNow = cal.get(Calendar.MONTH);  
          int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);  
          cal.setTime(birthDay);   
    
          int yearBirth = cal.get(Calendar.YEAR);  
          int monthBirth = cal.get(Calendar.MONTH);  
          int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);   
    
          int age = yearNow - yearBirth;  
    
          if (monthNow <= monthBirth) {  
              if (monthNow == monthBirth) {  
                  if (dayOfMonthNow < dayOfMonthBirth) age--;  
              }else{  
                  age--;  
              }  
          }  
          return age;  
      }  
      
      //获取当前年
      public static String getNowyear(){
    	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    	  Date date = new Date();
    	  return sdf.format(date);
      }
      
      //转字符日期转日期格式
      public static Date toDateFormat(String str) throws Exception{
    	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	  Date date = sdf.parse(str);
		  // 将接收的时间转换成SimpleDateFormat格式
		  String todateStr = sdf.format(date);
		  // 将String格式的时间转换成Date类型
		  Date dateObj = sdf.parse(todateStr);
		  return dateObj;
      }
      
   // 获取当前年份
      public static String getNowYear(){
	  	  Calendar date = Calendar.getInstance();        
	  	  String payTime = String.valueOf(date.get(Calendar.YEAR)); 
    	  return payTime;
      }
      
      
      /**
       * 生成当前日期字符串
       * @param args
       */
      public static String createNowDateStr(){
    	  Date d = new Date();
    	  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    	  String now = df.format(d);
    	  return now;
      }
     
      
      public static void main(String args[]) { 
       /*    try {
              int  age = getAge(parse("1996-12-04"));
              System.out.println("age=="+age);
          } catch (Exception e) {
              e.printStackTrace();
          } 
*/
          
      }
}
